package com.ifly.controller;

import com.ifly.entity.Emp;
import com.ifly.service.EmpService;
import lombok.extern.slf4j.Slf4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("emp")
//@Slf4j       //自定义logback时，引入注解会简化很多
public class EmpController {

    private static final Logger logger=LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private EmpService empService;

    //保存员工信息
    @PostMapping("add")
    public String add(Emp emp){
        empService.add(emp);
        //直接  log.日志级别
        //log.info("emp信息:"+emp);
        logger.info("emp信息:"+emp);
        return "redirect:/emp/findall";
    }

    //删除员工信息
    @GetMapping("delete")
    public String delete(String id){
        empService.delete(id);
        return "redirect:/emp/findall";
    }

    //查询员工信息
    @GetMapping("findall")
    public String findAll(Model model){
        List<Emp> emps=empService.findAll();
        model.addAttribute("emps",emps);
        return "/ems/emplist";
    }

    //根据id查询员工信息
    @GetMapping("findById")
    public String findById(String id,Model model){
        Emp emp = empService.findById(id);
        model.addAttribute("emp",emp);
        return "/ems/updateEmp";
    }

    //更新员工信息
    @PostMapping("update")
    public String update(Emp emp){
        empService.update(emp);
        return "redirect:/emp/findall";
    }


}
